package com.example.hrmsaas.company.controller;


import com.example.hrmsaas.company.entity.Department;
import com.example.hrmsaas.company.service.IDepartmentService;
import com.example.hrmsaas.company.vo.DeptVo;
import com.example.hrmsaas.system.controller.BaseController;
import com.example.hrmsaas.system.utils.DataGridView;
import com.example.hrmsaas.system.utils.DateToTime;
import com.example.hrmsaas.system.utils.IdWorker;
import com.example.hrmsaas.system.utils.ResultObject;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 雷哥
 * @since 2021-11-02
 */
@RestController
@RequestMapping("/company-dept")
public class DepartmentController extends BaseController {

    private Log log = (Log) LogFactory.getLog(DepartmentController.class);

    @Autowired
    private IDepartmentService departmentService;

    private Department department = new Department();

    @RequestMapping("/deptList")
    public DataGridView getDepartmentTree() throws ParseException {
        try {
            DeptVo deptVo = new DeptVo();
            Integer page = request.getParameter("page") !=null?Integer.valueOf(request.getParameter("page")):null;
            Integer limit = request.getParameter("limit")!=null?Integer.valueOf(request.getParameter("limit")):null;
            DeptVo DeptVoQueryMapper = this.getDeptVoQueryMapper(deptVo, page, limit,CompanyId);
            System.out.println(DeptVoQueryMapper);
            return departmentService.getDeptAll(DeptVoQueryMapper);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new DataGridView();
        }

    }

    private DeptVo getDeptVoQueryMapper(DeptVo deptVo, Integer page, Integer limit,String CompanyId) {
        if(null!=page)deptVo.setPage(page);
        if(null!=limit)deptVo.setLimit(limit);
        deptVo.setCompanyId(CompanyId);
        deptVo.setIsDel(0);
        return deptVo;
    }

    /***
     * 添加部门
     * @param
     * @return
     * @throws ParseException
     */
    @RequestMapping(value="/add",method = RequestMethod.POST)
    public ResultObject addDepartment(@RequestBody Department department) throws ParseException {
        //TODO 先默认位1，后期解决
        department.setCompanyId(this.CompanyId);
        department.setCreateTime(DateToTime.timestamToDatetime(System.currentTimeMillis()));
        department.setIsDel(0);
        IdWorker idWorker = new IdWorker();
        String id = idWorker.nextId()+"";
        department.setId(id);
        if (departmentService.save(department)) {
            return ResultObject.ADD_SUCCESS;
        } else {
            Exception e = new Exception();
            log.error("添加部门异常：" + e.getMessage());
            return ResultObject.ADD_ERROR;
        }


    }

    @RequestMapping(value="/edit",method = RequestMethod.POST)
    public ResultObject EditDepattment(@org.jetbrains.annotations.NotNull @RequestBody Department dept) throws ParseException {
        try {
            Department OldDept = departmentService.getDeptById(dept.getId());
            department.setCode(dept.getCode() == null ? OldDept.getCode() : dept.getCode());
            department.setPid(dept.getPid() == null ? OldDept.getPid() : dept.getPid());
            department.setCompanyId(this.CompanyId);
            department.setName(dept.getName() == null ? OldDept.getName() : dept.getName());
            department.setIntroduce(dept.getIntroduce() == null ? OldDept.getIntroduce() : dept.getIntroduce());
            department.setManager(dept.getManager()== null ? OldDept.getManager() : dept.getManager());
            department.setManagerId(dept.getManagerId() == null ? OldDept.getManagerId() : dept.getManagerId());
            department.setId(dept.getId());
            if (departmentService.EditDept(department)) {
                return ResultObject.SAVE_SUCCESS;
            } else {
                Exception e = new Exception();
                log.error("更新部门异常：" + e.getMessage());
                return ResultObject.SAVE_ERROR;
            }
        } catch (Exception e) {
            log.error("更新部门异常：" + e.getMessage());
            return ResultObject.SAVE_ERROR;
        }

    }

    /**
     * 获取部门详情
     * @param
     * @return
     * @throws ParseException
     */
    @RequestMapping("/info")
    public DataGridView getDeptInfo() throws ParseException {
        if(null != request.getParameter("id")){
            Department data = departmentService.getDeptById(request.getParameter("id"));
            return new DataGridView(data);
        }else{
            return new DataGridView();
        }
    }
    /**
     * 逻辑删除部门
     */
    @RequestMapping("del")
    public ResultObject delDepartment() throws ParseException {
        if(null != request.getParameter("id")){
            if(departmentService.DelDeptById(request.getParameter("id"))){
                return ResultObject.DELETE_SUCCESS;
            }else{
                return ResultObject.DELETE_SUCCESS;
            }
        }else{
            return new ResultObject(404, "缺少参数");
        }
    }

}

